package run.core;
import java.awt.Color;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

import run.Cell;
import run.Facet;
import run.StrFacet;

/** String Cell
 * 
 * @author Rajesh.ramakrishnan
 */
public class GUI extends Cell {
	private Facet in;
	private StrFacet out;
	private String myValue;
	public static int[][] grid;
	public static int count = 0;
	public static JFrame frame;
	public static Container pane;
	public static int width = 600;
	public static int height = 600;
	
	public GUI(String name) {
		super("GUI", name);
		myValue = name;
		in = new Facet(".in", this, INPUT_FACET);
		out = new StrFacet(".out", this, OUTPUT_FACET, name);
		
		if(grid == null){
			grid = new int[15][15];
		}
		
		if(frame == null){
			frame = new JFrame();
			frame.setSize(width, height);
			frame.setTitle("Game of Life");
			pane = frame.getContentPane();
			pane.setLayout(null);
			pane.setBackground(Color.PINK);
		}
	}

	/*@Override
	public void feedInput(Object data) {
		
	}*/

	/**
	 * Hacked up to PRINT when stimulated.
	 */
	@Override
	public List<Facet> stimulate(Facet stimmer)
	{
		ArrayList<Facet> doneFacets = new ArrayList<Facet>();
		List<Object> vals = in.dispatchOutbound();
//ignore nils
		
		Object concat = vals.get(0);
		if (!(concat instanceof NIL)) {
			if(!myValue.equals("")){
				count++;
				String[] values = myValue.split(",");
				grid[Integer.parseInt(values[0])][Integer.parseInt(values[1])] = Integer.parseInt(concat.toString());
				//System.out.println(myValue + ':' + concat);
				out.concat(concat);
				doneFacets.add(out);	
			}
		}
//		else System.out.println("NIL TO STR");
		if(count == ((15*15) - 1 )){
			frame.setVisible(true);
			//String output = "";
			pane.removeAll();
			for(int i = 0; i < grid.length; i++){
				for(int j = 0; j < grid[i].length; j++){
					//output += grid[i][j];
					JPanel panel = new JPanel();
					panel.setBounds(j*40, i*40, 40, 40);
					pane.add(panel);
					if(grid[i][j] != 0){
						panel.setBackground(Color.GREEN);
					}
					else{
						panel.setBackground(Color.PINK);
					}
				}
				//output += "\n";
			}
			count = 0;
			try{
				Thread.sleep(200);	//slow down the animation
			}
			catch(Exception ex){}
			pane.repaint();
			frame.repaint();
		}
		return doneFacets;
	}
	
	protected String injectInt(String s, int val) { return s.replaceFirst("%d", ((Integer)val).toString()); }
	protected String inject(String s, Object o) { return s.replaceFirst("%s", o.toString()); }
	
	/** Find and fix special characters.
	 * 
	 * @author Rajesh.ramakrishnan
	 */
	protected List<Object> process(String nm) {
		ArrayList<Object> vals = new ArrayList<Object>();
		String tmp = nm.replaceAll("\\\\n", "\n");
		tmp = tmp.replaceAll("\\\\t", "\t");
		tmp = tmp.replaceAll("\\\\b", "\b");
		tmp = tmp.replaceAll("\\\\f", "\f");
		tmp = tmp.replaceAll("\\\\r", "\r");
		tmp = tmp.replaceAll("\\\\u", "\r");
		tmp = tmp.replaceAll("\\\\%", "%");
		vals.add(tmp);
		return vals;
	}

	//Dot printout method
	public String toDot()
	{
		String returnString = "";
		returnString += "subgraph " + hashCode() + "{\n";
		returnString += "label = \"" + cellTypeName + "\";\n";
		returnString += "}\n";
		return returnString;
	}
	
	
	public List<Facet> getFacets()
	{
		ArrayList<Facet> temp = new ArrayList<Facet>();
		temp.add(in);
		temp.add(out);
		return temp;
	}
		
	public List<Facet> getOutFacets() {
		ArrayList<Facet> arrayList = new ArrayList<Facet>();
		arrayList.add(out);
		return arrayList;
	}
	
	@Override
	public boolean isLiteral() {
		return false;
	}
}

